package com.example.zbh.dsliao.ui.activty;

import android.content.Intent;
import android.text.Layout;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Toast;

import com.easemob.chat.EMChatManager;
import com.easemob.exceptions.EaseMobException;
import com.example.zbh.dsliao.R;
import com.example.zbh.dsliao.db.DatabaseHelper;
import com.example.zbh.dsliao.db.entity.UserContact;
import com.example.zbh.dsliao.model.entity.ContactPeople;
import com.example.zbh.dsliao.ui.adapter.ContactAdapter;
import com.example.zbh.dsliao.view.CustomChangeDialog;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 点击转发 跳转到的选择联系人页面
 */
public class TransmitActivity extends BaseActivity implements View.OnClickListener, ContactAdapter.OnlongClickItemLisener {

    private ContactAdapter contactAdapter;//跟通讯录页面用的同一个适配器
    private ListView listView;
    private LinearLayout dl_commom_back_btn;
    private List<ContactPeople> listContact = new ArrayList<>();//联系人列表
    private List<String> exitingMembers = new ArrayList<String>();//
    private String username, Msg_Id;
    

    @Override
    protected int getLayOut() {
        return R.layout.activity_transmit;
    }

    @Override
    protected void initView() {
        listView = findeView(R.id.lv_transmit);
        dl_commom_back_btn = findeView(R.id.back);

    }

    @Override
    protected void initData() {
        Msg_Id = getIntent().getStringExtra("Msg_Id");
        dl_commom_back_btn.setOnClickListener(this);
        List<String> friendList = null;
        try {
            friendList = EMChatManager.getInstance().getContactUserNames();
        } catch (EaseMobException e) {
            e.printStackTrace();
        }
        for (int i = 0; i < friendList.size(); i++) {
         UserContact userContact= DatabaseHelper.getInstance().queryContactPeopleByName(friendList.get(i));
            if (userContact != null){
                String nickName = userContact.getNickname();
                if (nickName != null){
                    friendList.remove(friendList.get(i));
                    friendList.add(nickName);
                }
            }
        }

        for (int i = 0; i < friendList.size(); i++) {
            listContact.add(new ContactPeople(friendList.get(i)));
        }
        Collections.sort(listContact);
        contactAdapter = new ContactAdapter(this);
        listView.setAdapter(contactAdapter);
        contactAdapter.addData(listContact);

        contactAdapter.setOnlongClickItemLisener(this);//接口回调 联系人点击事件

    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.back://返回
                finish();
                break;
        }
    }

    /**
     * 点击联系人弹出dialog
     * **/
    @Override
    public void onlongClickItem(View view, int code, int position) {
        username = (String) contactAdapter.getItem(position);
        if (code == 1){
            showDialog(view, code, position);
        }
    }

    private void showDialog(View view, int code, int position) {
        final CustomChangeDialog customChangeDialog = new CustomChangeDialog(this, "您确定要转发给:" + username + "么?", View.GONE, "", "确定!", "取消");
        customChangeDialog.show();
        customChangeDialog.setCustomChangeDialogListener(new CustomChangeDialog.CustomChangeDialogListener() {
            @Override
            public void onClick(View view) {
                switch (view.getId()){
                    case R.id.changeTwoBtn:
                        customChangeDialog.dismiss();
                        Intent intent = new Intent(TransmitActivity.this, CharActivity.class);
                        intent.putExtra("userName", username);
                        Log.d("TransmitActivity", username);
                        intent.putExtra("Msg_Id_T", Msg_Id);
                        startActivity(intent);
                        finish();
                        break;
                    case R.id.cancleBtn:
                        customChangeDialog.dismiss();
                        break;
                }
            }
        });

    }


}
